layui.define(['element', 'tab', 'utils','resource_toolbar'], function(exports) {
	"use strict";

	/**
	 * Button component
	 * */
	var MOD_NAME = 'resource_tabs',
		$ = layui.jquery,
        pearTab = layui.tab,
        utils = layui.utils,
        resource_tabs_obj,
        resource_toolbar = layui.resource_toolbar;

	var resource_tabs = function(opt) {
		this.option = opt;
	};


    function resetContent(contentElem){
        var parent = $(contentElem).parent();
        $(contentElem).remove();
        parent.append('<div id="'+contentElem.slice(1)+'"></div>')
    }



    function tabsRender(viewMode){
        var pearBody;
        // 加载右侧
        if(viewMode === 'tabs'){
            $('#fast-body').hide()
            var tab_elem = 'content';
            pearBody = pearTab.render({
                
                elem: tab_elem,
                removeOtherClass: true,
                roll: true,
                tool: true,
                width: '100%',
                height: '100%',
                session: false,
                index: 0,
                tabMax: 20,
                // removeOtherContent: true,
                // removeOtherClass: true,
                preload: true,
                closeEvent: function(id) {

                },
                data: [{
                    id: 'dashbord',
                    mode:'dom',
                    // domContent:'<div>首页</div>',
                    domContent:'<div><img style="width:100%;" src="'+window.rootPath.replace('/js', '/image')+'fast-dev.jpg"></div>',
                    url: 'none',
                    title: '首页',
                    close: false
                }],
                success: function(id) {
                    // console.log(id,'ididid1111111')
                },
                refersh: function(id){
                    refresh(id)
                }
            })
            pearBody.click(function(id) {
                // debugger

                refresh(id);
                utils.setTreeNode(id)
                console.log(id,'id')
                
                setTimeout(function(){
                    var treeId = sessionStorage.getItem("content-pear-tab-data-current")
                    treeId = treeId.slice(0,treeId.indexOf('_'))
                    var treeNode =  JSON.parse(sessionStorage.getItem("content-pear-current-treeNode"))
                    if(treeId && treeNode){
                        resource_toolbar.resource_toolbar_resourceTreeClickListener(null,treeId,treeNode)
                    }
                },600)
                
                // var currentTreeId = sessionStorage.getItem("currentTreeId")
                // var treeNode = $.fn.zTree.getZTreeObj(currentTreeId).getNodeByTId(id)
                // sessionStorage.setItem('content-pear-current-treeNode',JSON.stringify(treeNode))
                // console.log(treeNode,'treeNode')
                //
                // var tree_a_dom_id = treeNode.tId + "_a";
                // $('#'+ tree_a_dom_id).trigger("click");

            })
        } else {
            $('#content').hide()
        }

        return pearBody;
    }

    /**
	 * Button start loading
	 * */
    resource_tabs.prototype.render = function(opt) {
		
		var option = {
			elem: opt.elem,
            viewMode: opt.viewMode,
            
		}


        //因刷新树时会清空你tab，所以该方法单独抽出。
        var pearBody = tabsRender(option.viewMode);

        option.pearBody = pearBody;

        resource_tabs_obj = new resource_tabs(option);
        return resource_tabs_obj;
	}

    /**
     * 增加tab
     * @param opt
     * @returns {*}
     */
    resource_tabs.prototype.addTab = function(opt){
        //从opt中输入的配置
        var {id, title, data, contentFn} = opt;

        var option = this.option;
        var viewMode = option.viewMode;
        var pearBody = option.pearBody;
        if(viewMode === 'tabs'){
            $('#fast-body').hide()
            $('#content').show()

            var domId = "treenode_" + id;
            pearBody.addTabOnly({
                    mode: 'dom',
                    content: '<div id="'+domId+'"></div>',
                    close: true,
                    title: title,
                    id: id
                }
            )
            sessionStorage.setItem( "content-pear-current-treeNode", JSON.stringify(data));
        } else {
            $('#fast-body').show()
            $('#content').hide()
            $('#content .layui-tab-content .layui-tab-item').each(function(index){
                if(index !== 0){
                    $(this).remove()
                }
            })
            $('#content .layui-tab-title li').each(function(index){
                if(index !== 0){
                    $(this).remove()
                }
            })
        }

        var tabElem = utils.getTabElem(id);
        if(!utils.contentDomIsExist(tabElem)){
            if(opt.viewMode === 'body'){
                resetContent(option.elem);
            }

            //执行渲染tab内容
            contentFn.fn(contentFn.data);

            //缓存tabId所对应的内容渲染函数
            if(!option.tabIdContentFn){
                option.tabIdContentFn = {};
            }
            option.tabIdContentFn[tabElem] = contentFn;
        }

        return tabElem;
    }

    function refresh(tabId){
        var tabElem = utils.getTabElem(tabId);
        resetContent(tabElem);
        var option = resource_tabs_obj.option;

        if(option.tabIdContentFn){
            var contentFn = option.tabIdContentFn[tabElem];
            if(contentFn){
                //执行渲染tab内容
                contentFn.fn(contentFn.data);
            }

        }

    }

    /**
     * 刷新tab内容
     * @param tabId
     */
    resource_tabs.prototype.refresh = function(tabId){
        refresh(tabId);
    }

    exports(MOD_NAME, new resource_tabs());
});
